package com.qj.yantaishan.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qj.yantaishan.common.exception.ResponseException;
import com.qj.yantaishan.common.exception.UnLogin;
import com.qj.yantaishan.common.utils.StringUtils;
import com.qj.yantaishan.domain.AutoLogin;
import com.qj.yantaishan.mapper.AdminMapper;
import com.qj.yantaishan.mapper.AuthLoginMapper;
import com.qj.yantaishan.service.AuthLoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j


public class AuthInterceptor implements HandlerInterceptor {


    @Autowired
    AuthLoginMapper authLoginMapper;
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    AuthLoginService authLoginService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("登录拦截开启");
        String token = request.getHeader("token");
        if (StpUtil.getLoginIdByToken(token) != null) {
            return true;
        }
//        if (!StringUtils.isNotEmpty(token)) {
//            log.error("没有token，进行拦截！");
//            throw new UnLogin();
//        }
//        AutoLogin auth = authLoginMapper.selectOne(new QueryWrapper<AutoLogin>().eq("token", token));
//        if (auth == null) {
//            log.error("token错误，进行拦截！");
//            throw new UnLogin();
//
//
//        }
//        if (token != null && auth != null) {
//            request.setAttribute(token, auth);
//            log.info("token正常，放行");
//        }
        throw new UnLogin();
//        return false;
    }
}
